package com.sdy.dcsb.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.model.Response;
import com.sdy.dcsb.biz.model.EsbConsumerSystemIpGroup;
import com.sdy.dcsb.biz.service.EsbConsumerSystemIpGroupService;
import com.sdy.dcsb.biz.service.UserOperationLogService;
import com.sdy.mvc.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URLDecoder;
import static org.springframework.util.StringUtils.isEmpty;

/**
 * <p>
 * 服务消费者系统下群组信息表 前端控制器
 * </p>
 *
 * @author fb
 * @since 2019-09-29
 */
@Slf4j
@Controller
@RequestMapping("/EsbConsumerSystemIpGroup")
@Api(tags = "消费者系统群组信息相关接口")
public class EsbConsumerSystemIpGroupController extends BaseController {
    @Autowired
    private EsbConsumerSystemIpGroupService esbConsumerSystemIpGroupService;
    @Autowired
    private UserOperationLogService userOperationLogService;

    /**
     * 系统下群组信息，分页数据，可按群组名称模糊查询
     * @param id 当前系统id
     * @param message ,按群组名称模糊查询，查询值
     * @param page [current, size]
     * @return
     */
    @GetMapping("/pageData")
    @ResponseBody
    @ApiOperation("系统下群组信息，分页数据，可按群组名称模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "id", value = "当前系统id"),
            @ApiImplicitParam(dataType = "String", name = "message", value = "按群组名称模糊查询，查询值"),
            @ApiImplicitParam(dataType = "String", name = "current", value = "当前页"),
            @ApiImplicitParam(dataType = "String", name = "size", value = "每页数量"),
    })
    public Response pageData(Integer id, String message, Page<EsbConsumerSystemIpGroup> page) {
        if(isEmpty(id)) {
            return Response.error("请求id不能为空");
        }
        LambdaQueryWrapper<EsbConsumerSystemIpGroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EsbConsumerSystemIpGroup::getSystemid,id);
        wrapper.orderByDesc(EsbConsumerSystemIpGroup::getGroupid);
        if(!isEmpty(message)) {
            try {
                message = URLDecoder.decode(message,"utf-8");
            }catch(IOException e) {
                log.error(e.getMessage());
            }
            wrapper.like(EsbConsumerSystemIpGroup::getGroupname,message);
        }
        IPage<EsbConsumerSystemIpGroup> result = esbConsumerSystemIpGroupService.page(page, wrapper);
        return Response.success(result);
    }

    /**
     * 查询单条群组信息
     * @param groupid ,当前群组groupid
     * @return
     */
    @GetMapping("/select")
    @ResponseBody
    @ApiOperation("查询单条群组信息")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "groupid", value = "当前群组groupid"),
    })
    public Response select(Integer groupid) {
        if(!isEmpty(groupid)){
            return Response.success(esbConsumerSystemIpGroupService.getById(groupid));
        }else {
            return Response.error("群组groupid不能为空");
        }
    }

    /**
     * 修改当前群组信息
     * @param groupid ,当前群组id
     * @param groupname ,群组名称
     * @param groupcode ,群组编码
     * @param appowner ,群组负责人
     * @return
     */
    @GetMapping("/update")
    @ResponseBody
    @ApiOperation("修改当前群组信息")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "groupid", value = "当前群组id"),
            @ApiImplicitParam(dataType = "String", name = "groupname", value = "群组名称"),
            @ApiImplicitParam(dataType = "String", name = "groupcode", value = "群组编码"),
            @ApiImplicitParam(dataType = "String", name = "appowner", value = "群组负责人"),
    })
    public Response updateName(HttpServletRequest request, Integer groupid, String groupname, String groupcode, String appowner) {
        if(!isEmpty(groupname) || !isEmpty(groupid)) {
            try {
                groupname = URLDecoder.decode(groupname,"utf-8");
                if(!isEmpty(groupcode)) {
                    groupcode = URLDecoder.decode(groupcode,"utf-8");
                }
                if(!isEmpty(appowner)) {
                    appowner = URLDecoder.decode(appowner,"utf-8");
                }
            }catch(IOException e) {
                log.error(e.getMessage());
            }
            EsbConsumerSystemIpGroup esbConsumerSystemIpGroup = new EsbConsumerSystemIpGroup();
            esbConsumerSystemIpGroup.setGroupname(groupname);
            esbConsumerSystemIpGroup.setGroupcode(groupcode);
            esbConsumerSystemIpGroup.setAppowner(appowner);
            userOperationLogService.savaUserLog(request, "修改消费者群组信息：" +
                    esbConsumerSystemIpGroupService.getById(groupid).getGroupname());
            return Response.success(esbConsumerSystemIpGroupService.update(esbConsumerSystemIpGroup,
                    new UpdateWrapper<EsbConsumerSystemIpGroup>().eq("groupid", groupid)));
        }else {
            return Response.error("群组名称或者群组id不能为空");
        }
    }

    /**
     * 在当前系统下新增群组
     * @param systemid ,当前系统id
     * @param groupname ,群组名称
     * @param groupcode ,群组编码
     * @param appowner ,群组负责人
     * @return
     */
    @GetMapping("/insert")
    @ResponseBody
    @ApiOperation("在当前系统下新增群组")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "systemid", value = "当前系统id"),
            @ApiImplicitParam(dataType = "String", name = "groupname", value = "群组名称"),
            @ApiImplicitParam(dataType = "String", name = "groupcode", value = "群组编码"),
            @ApiImplicitParam(dataType = "String", name = "appowner", value = "群组负责人"),
    })
    public Response insert(HttpServletRequest request, Integer systemid, String groupname, String groupcode, String appowner) {
        if(!isEmpty(groupname) || !isEmpty(systemid)) {
            try {
                groupname = URLDecoder.decode(groupname,"utf-8");
                if(!isEmpty(groupcode)) {
                    groupcode = URLDecoder.decode(groupcode,"utf-8");
                }
                if(!isEmpty(appowner)) {
                    appowner = URLDecoder.decode(appowner,"utf-8");
                }
            }catch(IOException e) {

            }
            EsbConsumerSystemIpGroup esbConsumerSystemIpGroup = new EsbConsumerSystemIpGroup();
            esbConsumerSystemIpGroup.setSystemid(systemid);
            esbConsumerSystemIpGroup.setGroupname(groupname);
            esbConsumerSystemIpGroup.setGroupcode(groupcode);
            esbConsumerSystemIpGroup.setAppowner(appowner);
            userOperationLogService.savaUserLog(request, "新增消费者群组信息：" + groupname);
            return Response.success(esbConsumerSystemIpGroupService.save(esbConsumerSystemIpGroup));
        }else {
            return Response.error("群组名称或者系统id不能为空");
        }
    }

    /**
     * 删除群组信息
     * @param groupid ,当前群组id
     * @return
     */
    @GetMapping("/delete")
    @ResponseBody
    @ApiOperation("删除群组信息")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "groupid", value = "当前群组id"),
    })
    public Response delete(HttpServletRequest request, Integer groupid) {
        if(!isEmpty(groupid)){
            userOperationLogService.savaUserLog(request, "删除消费者群组信息：" +
                    esbConsumerSystemIpGroupService.getById(groupid).getGroupname());
            return Response.success(esbConsumerSystemIpGroupService.removeById(groupid));
        }else {
            return Response.error("群组groupid不能为空");
        }
    }

}
